package com.dengqi.mymodule.activity.loader.applists;

import android.content.pm.ApplicationInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dengqi.mymodule.R;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by dell on 2019/5/7
 */
public class AppListAdapter extends RecyclerView.Adapter<AppListAdapter.AlbumViewHolder> {

    private List<ApplicationInfo> datas = new ArrayList<>();

    public AppListAdapter(List<ApplicationInfo> datas) {
        this.datas = datas;
    }

    @Override
    public AlbumViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album, null);
        return new AlbumViewHolder(view);
    }

    @Override
    public void onBindViewHolder(AlbumViewHolder holder, int position) {
        String className = datas.get(position).className;
        int icon = datas.get(position).icon;
        String name = datas.get(position).name;

        holder.tvAlbumName.setText(className);
        holder.tvAlbumAmount.setText(name);
        Glide.with(holder.ivAlbum.getContext())
                .load(icon)
                .into(holder.ivAlbum);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public static class AlbumViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivAlbum;
        private TextView tvAlbumName;
        private TextView tvAlbumAmount;

        public AlbumViewHolder(View itemView) {
            super(itemView);
            ivAlbum = (ImageView) itemView.findViewById(R.id.album_iv_album);
            tvAlbumName = (TextView) itemView.findViewById(R.id.album_tv_album_name);
            tvAlbumAmount = (TextView) itemView.findViewById(R.id.album_tv_amount);
        }
    }
}